package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.service.FileService;
import com.sun.prism.shader.Mask_TextureSuper_AlphaTest_Loader;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jodd.io.upload.FileUpload;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@Api(tags = "文件管理")
@RestController
@RequestMapping("/admin/product")
public class FileController {

    @Autowired
    FileService fileService;

    @ApiOperation("上传文件")
    @PostMapping("fileUpload")
    public Result FileUpload(@RequestPart("file") MultipartFile file) throws Exception {

        String filename = file.getOriginalFilename();
        String contentType = file.getContentType();
        log.info("文件上传：文件名：{}，文件大小：{}，文件类型：{}",filename,file.getSize(),contentType);

        String path = fileService.upload(file);

        return Result.ok(path);


    }
}
